package org.glutenberg.util;

import java.util.Enumeration;

import org.glutenberg.util.binarytree.SymmetricTraversal;
import org.glutenberg.util.binarytree.Tree;

public class TreeSet {
	
	private Tree tree;
	
	public TreeSet(){
		this(null);
	}
	
	public TreeSet(Comparator comparator){
		this.tree = new Tree(null, comparator, false);
	}
	
	public void add(Object c){
		this.tree.insert(c);
	}
	
	public boolean contains(Object key){
		return this.tree.searchFirst(key)!=null;
	}
	
	public Enumeration elements(){
		return new SymmetricTraversal(this.tree);
	}
	
	public int size(){
		return this.tree.size();
	}
	
	public TreeSet intersection(TreeSet other){
		Enumeration enum = this.elements();
		TreeSet ret = new TreeSet();
		while(enum.hasMoreElements()){
			Object o = enum.nextElement();
			if(other.contains(o)) ret.add(o);
		}
		return ret;
	}
}
